package com.handinfo.android.core.input;

import android.view.MotionEvent;

import com.handinfo.android.core.graphics.DWGraphics;

public class DWDPad
{
	public DWDPad()
	{
	}

	private int mPointId = -1;

	public int getmPointId()
	{
		return mPointId;
	}

	public void setmPointId(int mPointId)
	{
		this.mPointId = mPointId;
	}

	public float mSmallCenterX = 100, mSmallCenterY = 380, mSmallCenterR = 30;
	public float mBigCenterX = 100, mBigCenterY = 380, mBigCenterR = 100;

	public static final int TOUCH_UP = 1;
	public static final int TOUCH_DOWN = 2;
	public static final int TOUCH_LEFT = 3;
	public static final int TOUCH_RIGHT = 4;

//	private static final int MAX_TOUCHPOINT = 2;

	//画摇杆
	public void draw(DWGraphics g)
	{
		g.setColor(0x77, 255, 0, 0);
		g.fillCircle(mBigCenterX, mBigCenterY, mBigCenterR);
		g.fillCircle(mSmallCenterX, mSmallCenterY, mSmallCenterR);
	}

	public void onTouch(MotionEvent event)
	{

		int actionId = event.getAction();
		//获取触控点的数量
		int pointerCount = event.getPointerCount();
		int id = 0;
		float x = 0, y = 0;
		//通过一个循环执行getPointerId方法获取索引
		for (int i = 0; i < pointerCount; i++) {
			id = event.getPointerId(i);
			x = event.getX(i);
			y = event.getY(i);
			//触控动作
			switch (actionId) {
				//按下手指
				case MotionEvent.ACTION_DOWN:
					if (Math.sqrt(Math.pow((mBigCenterX - x), 2)
							+ Math.pow((mBigCenterY - y), 2)) <= mBigCenterR) {
						//让小圆跟随用户触点位置移动
						mSmallCenterX = x;
						mSmallCenterY = y;
						mPointId = id;
					}
					break;
				//抬起手指
				case MotionEvent.ACTION_UP:
					if (mPointId == id) {
						mSmallCenterX = mBigCenterX;
						mSmallCenterY = mBigCenterY;
					}
					break;
				//移动手指
				case MotionEvent.ACTION_MOVE:
					if (Math.sqrt(Math.pow((mBigCenterX - x), 2)
							+ Math.pow((mBigCenterY - y), 2)) <= mBigCenterR) {
						//让小圆跟随用户触点位置移动
						mSmallCenterX = x;
						mSmallCenterY = y;
					}
					break;
				//抬起手指的第一个动作
				case MotionEvent.ACTION_POINTER_UP:
					if (mPointId == (actionId & MotionEvent.ACTION_POINTER_ID_MASK)) {
						mSmallCenterX = mBigCenterX;
						mSmallCenterY = mBigCenterY;
					}
					break;
			}
		}
	}

	public int onTouch()
	{
//		if (370 < mSmallCenterY && mSmallCenterY < 420) {
//			return TOUCH_UP;
//		}
//		if (420 < mSmallCenterY && mSmallCenterY < 470) {
//			return TOUCH_DOWN;
//		}
//		if (0 < mSmallCenterX && mSmallCenterX < 100) {
//			return TOUCH_LEFT;
//		}
//		if (50 < mSmallCenterX && mSmallCenterX < 100) {
//			return TOUCH_RIGHT;
//		}
		return -1;
	}

	public boolean isUpPressed()
	{
		return onTouch() == TOUCH_UP;
	}

	public boolean isDownPressed()
	{
		return onTouch() == TOUCH_DOWN;
	}

	public boolean isLeftPressed()
	{
		return onTouch() == TOUCH_LEFT;
	}

	public boolean isRightPressed()
	{
		return onTouch() == TOUCH_RIGHT;
	}
}
